<template>
    <div class="left-wrapper">
        <el-table :data="emails" ref="tableRef" row-key="id" @row-click="handleRowClick" :show-header="false"
            highlight-current-row class="list-wrapper">
            <el-table-column type="selection" width="55" />
            <el-table-column>
                <template #default="{ row }">
                    <div class="item">
                        <div class=" subject flex">
                            <span>{{ row.subject }}</span>
                            <dict-tag :options="email_status" :value="row.status" />
                        </div>
                        <div class="extra">
                            <el-text line-clamp="2">
                                {{ row.abstract }}
                            </el-text>
                        </div>
                        <div class="flex">
                            <el-text type="info" v-if="row.status == 0">
                                更新于：{{ parseTime(row.updatedAt) }}
                            </el-text>
                            <el-text type="info" v-else-if="row.status == 1">
                                发送于：{{ parseTime(row.sentTime) }}
                            </el-text>
                        </div>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script setup>
import { email_status } from "../status"
const emails = defineModel({
    type: Array,
    default: []
})
const currentEmail = ref();
const tableRef = ref();
const emits = defineEmits(["mailChange"])
const props = defineProps({
    beforeChange: {
        type: Function,
        default: () => true
    }
})

/**设置当前行 */
const setCurrentRow = (row) => {
    tableRef.value.setCurrentRow(row);
}

/** 点击邮箱时 */
const handleRowClick = async (row) => {
    // 如果当前行已选中，直接返回
    if (currentEmail.value?.id === row.id) return;

    try {
        await props.beforeChange(row);
        currentEmail.value = row;
        emits("mailChange", row.id);
        tableRef.value.setCurrentRow(row);
        tableRef.value.clearSelection();
        tableRef.value.toggleRowSelection(row, true);
    } catch (error) {
        // 切换被阻止，恢复当前行
        tableRef.value.setCurrentRow(currentEmail.value);
        console.error('beforeChange 阻止了切换：', error);
    }
}


/**获取已选中邮件 */
const getSelectRows = () => {
    return tableRef.value.getSelectionRows();
}


defineExpose({ setCurrentRow, getSelectRows })

</script>

<style lang="scss" scoped>
:deep(.el-dropdown) {
    font-size: var(--el-font-size-extra-small);
}

.left-wrapper {
    height: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.action-wrapper {
    display: flex;
    margin-bottom: 20px;
    gap: 10px;
    align-items: center;

    .search-input {
        flex: 1;
    }
}

.list-wrapper {
    display: flex;
    gap: 12px;
    flex-direction: column;
    overflow-y: auto;

    .item {
        // padding: 16px;
        flex: 1;
        // border: 1px solid var(--el-border-color);
        cursor: pointer;
        display: flex;
        flex-direction: column;
        gap: 5px;

        .extra,
        .subject {
            display: flex;
            gap: 8px;
            align-items: center;
            justify-content: space-between;
        }

        .extra {
            text-indent: 2em;
        }

        .flex {
            display: flex;
            gap: 8px;
            align-items: center;
            justify-content: space-between;
        }
    }
}
</style>